package com.bigdata.booklibraryseatapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.booklibraryseatapi.common.exception.BusinessException;
import com.bigdata.booklibraryseatapi.common.exception.ResponseCode;
import com.bigdata.booklibraryseatapi.common.pojo.PageResult;
import com.bigdata.booklibraryseatapi.entity.BookTimeQuantum;
import com.bigdata.booklibraryseatapi.mapper.BookTimeQuantumMapper;
import com.bigdata.booklibraryseatapi.service.IBookTimeQuantumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.booklibraryseatapi.vo.req.AddTimeQuantumReqVO;
import com.bigdata.booklibraryseatapi.vo.req.EditTimeQuantumReqVO;
import com.bigdata.booklibraryseatapi.vo.req.PageBookTimeQuantumReqVO;
import com.bigdata.booklibraryseatapi.vo.resp.PageBookTimeQuantumRespVO;
import com.bigdata.booklibraryseatapi.vo.resp.TimeQuantumListRespVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 预约时间段 服务实现类
 * </p>
 *
 * @author book
 * @since 2023-12-13
 */
@Service
public class BookTimeQuantumServiceImpl extends ServiceImpl<BookTimeQuantumMapper, BookTimeQuantum> implements IBookTimeQuantumService {

    @Resource
    private BookTimeQuantumMapper bookTimeQuantumMapper;

    @Override
    public void addTimeQuantum(AddTimeQuantumReqVO vo) {
        BookTimeQuantum timeQuantum = new BookTimeQuantum();
        timeQuantum.setTimeQuantum(vo.getTimeQuantum());
        timeQuantum.setStatus(1);
        int result = bookTimeQuantumMapper.insert(timeQuantum);
        if(result!=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void editTimeQuantum(EditTimeQuantumReqVO vo) {
        BookTimeQuantum timeQuantum = bookTimeQuantumMapper.selectById(vo.getId());
        timeQuantum.setTimeQuantum(vo.getTimeQuantum());
        timeQuantum.setStatus(vo.getStatus());
        int result = bookTimeQuantumMapper.updateById(timeQuantum);
        if(result!=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public List<TimeQuantumListRespVO> getTimeQuantumList() {
        return bookTimeQuantumMapper.getTimeQuantumListRespVo();
    }

    @Override
    public PageResult<PageBookTimeQuantumRespVO> pageBookTimQuantum(PageBookTimeQuantumReqVO vo) {
        IPage<PageBookTimeQuantumRespVO> result = bookTimeQuantumMapper.selectPageBookTimeQuantum(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(), result.getTotal());
    }
}
